package com.yeyixiao.blog.service;

import com.yeyixiao.blog.dao.CommentDao;
import com.yeyixiao.blog.domain.Comment;
import com.yeyixiao.blog.domain.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
@Service
public class CommentService extends AbstractService<Comment>{
@Autowired
    private CommentDao entityDao;
    public Comment getById(Long id){
        return entityDao.getById(id);
    }

    public List<Comment> queryByArticle(Long id){
        return entityDao.getByArticle(id);
    }

    /*public Pagination<Comment> queryPage(Comment queryEntity ,Pagination<Comment> page){
        Integer itemsTotalCount = entityDao.getTotalItemsCount(queryEntity);
        List<Comment> items = entityDao.queryPage(queryEntity,page);
        page.setItemsTotalCount(itemsTotalCount);
        page.setItems(items);
        return page;
    }*/

    //基本信息初始化
    public void initComment(Comment entity){
        Integer art=getNewComment();
        entity.setId(art+1);
    }

    public void create(Comment entity){
        String date=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        System.out.println(date);
        entity.setDate(date);
        System.out.println(entity.getDate());
        entityDao.create(entity);
    }

    @Transactional
    public Comment createComment(Comment entity){
        initComment(entity);
        this.create(entity);

        this.update(entity);
        return entity;
    }

    public void update(Comment entity){
        //entity.setDate(new Date());
        System.out.println(entity.getDate());
        entityDao.update(entity);
    }

    public void delete(Comment entity){
        entityDao.delete(entity);
    }
    public Integer getNewComment(){
        List<Integer> list=entityDao.getNewComments();
        Integer maxint=0;

        Iterator<Integer> iterator=list.iterator();
        while(iterator.hasNext()){
            Integer news=iterator.next();
            if(news >maxint){
                maxint=news;

            }
        }
        return maxint;
    }
}
